package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.anno.Log;
import org.example.mapper.StudentMapper;
import org.example.pojo.Clazz;
import org.example.pojo.ClazzQueryParm;
import org.example.pojo.Result;
import org.example.service.imp.ClazzServiceImp;
import org.example.service.imp.StudentServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    ClazzServiceImp clazzServiceImp;
    @Autowired
    private StudentServiceImp studentServiceImp;
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 班级分页查询
     */
    @GetMapping
    public Result findByPage( ClazzQueryParm clazzQueryParm )

    {
        log.info("分页查询:{}", clazzQueryParm);
        return Result.success(clazzServiceImp.findByPage(clazzQueryParm));
    }
    /**
     * 删除班级
     */
    @Log
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/{id}")
    public Result delete(@PathVariable("id")  Integer id) {
        log.info("删除班级:{}", id);
        clazzServiceImp.deleteById(id);
        return Result.success();
    }
    /**
     * 添加班级
     */
    @Log
    @RequestMapping
    public Result save(@RequestBody Clazz clazz) {
        log.info("添加班级:{}", clazz);

        clazzServiceImp.save(clazz);
        return Result.success();
    }
    /**
     * 查询班级(ID)
     */

    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        log.info("查询班级:{}", id);
        return Result.success(clazzServiceImp.findById(id));
    }
    /**
     * 修改班级
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级:{}", clazz);
        clazzServiceImp.update(clazz);
        return Result.success();
    }
    /**
     * 查询所有班级
     */
    @GetMapping("/list")
    public Result findAll() {
        log.info("查询所有班级");
        return Result.success(clazzServiceImp.findAll());
    }
}
